package UIFree.controls
{
	import UIFree.uif_component;
	import UIFree.uif_transition;
	
	import flash.events.MouseEvent;

	public class uif_button extends uif_label {
		private var _buttonState: int;
		
		public function uif_button(parent: uif_component, 
					ax: int=0, ay: int=0, 
					awidth: int=0, aheight: int=0, styleClass: Object='') {
			super(parent, ax, ay, awidth, aheight, styleClass);
			
			_buttonState = 0;
			this.addEventListener(MouseEvent.MOUSE_OVER, doOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, doOut);
			this.addEventListener(MouseEvent.MOUSE_DOWN, doDown);
			this.addEventListener(MouseEvent.MOUSE_UP, doUp);
			this.addEventListener(MouseEvent.CLICK, doRelease);
			asButton();
		}
		
		public static function stdButton(parent: uif_component, ax: int, ay: int, onClick: Function, caption: String='ok', className:String='button', awidth:int=120, aheight:int=30): uif_button {
			var _button: uif_button = new uif_button(parent, ax, ay, awidth, aheight, className);
			_button.text = caption;
			_button.addEvent(MouseEvent.CLICK, onClick);
			return _button;
		}
		
		protected function get buttonState(): int {
			return _buttonState;
		}
		
		protected function set buttonState(new_state: int): void {
			if (_buttonState != new_state) {
				_buttonState = new_state;
				var cur_style: String = styleClass;
				switch (_buttonState) {
					case 1: cur_style = style['overClass']?style['overClass']:styleClass; break;
					case 2: cur_style = style['downClass']?style['downClass']:styleClass; break;
				}
//				style = app.styleByObj(cur_style);
				fx('style', style, app.styleByObj(cur_style)).start();
			}
		}
		
		protected function doOver(e: MouseEvent): void {
			if (!disable) buttonState = 1;
		}

		protected function doOut(e: MouseEvent): void {
			buttonState = 0;
		}

		protected function doDown(e: MouseEvent): void {
			if (!disable) buttonState = 2;
		}

		protected function doUp(e: MouseEvent): void {
			buttonState = 0;
		}
		
		protected function doRelease(e: MouseEvent): void {
		}
	}
}